package api

import (
	"github.com/gin-gonic/gin"
	"go-caipu/pkg/api/helper"
	"go-caipu/pkg/constant"
	"go-caipu/pkg/services/dto"
	"go-caipu/pkg/services/monitor/onlineuser"

	"go-caipu/pkg/services/admin/systemlog"
)

func (hs *HTTPServer) GetSysLoginLog(c *gin.Context) {
	cmd := systemlog.GetSysLoginLogCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	result, err := hs.systemLogService.Get(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithData(c, result)

}
func (hs *HTTPServer) CreateSysLoginLog(c *gin.Context) {
	cmd := systemlog.SysLoginLog{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	err := hs.systemLogService.InsertLoginLog(c.Request.Context(), &cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}

func (hs *HTTPServer) UpdateSysLoginLog(c *gin.Context) {
	cmd := systemlog.SysLoginLog{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	err := hs.systemLogService.Update(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}

func (hs *HTTPServer) RemoveSysLoginLog(c *gin.Context) {
	cmd := systemlog.RemoveSysLoginLogCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	err := hs.systemLogService.Remove(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}

func (hs *HTTPServer) GetSysOperalog(c *gin.Context) {
	cmd := systemlog.GetSysOperaLogCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	result, err := hs.systemLogService.GetSysOperaLog(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithData(c, result)
}

type OperaLog struct {
	CreateTime     []string `form:"createTime"`
	Module         string   `form:"module"`
	dto.Pagination `search:"-"`
	Status         string `form:"status"`
	Sort           string `form:"sort"`
}

// GetOperaLogHandle 获取操作记录包括（登录和操作）
func (hs *HTTPServer) GetOperaLogHandle(c *gin.Context) {
	cmd := OperaLog{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
		return
	}
	if len(cmd.CreateTime) != 2 {
		helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, constant.ErrInvalidParams)
		return
	}
	if cmd.Module != "" {
		//登录日志
		logCmd := systemlog.GetSysLoginLogPageCommand{
			BeginTime:  cmd.CreateTime[0],
			EndTime:    cmd.CreateTime[1],
			Pagination: cmd.Pagination,
		}
		hs.getSysLoginLogPage(c, logCmd)
		return
	}
	//操作日志
	operaLog := systemlog.GetSysOperaLogPageCommand{
		Pagination: cmd.Pagination,
		BeginTime:  cmd.CreateTime[0],
		EndTime:    cmd.CreateTime[1],
		Status:     cmd.Status,
		SysOperaLogOrder: systemlog.SysOperaLogOrder{
			//CreatedAtOrder: cmd.Sort,
			CreatedAtOrder: "desc",
		},
	}
	hs.getSysOperaLogPage(c, operaLog)
}
func (hs *HTTPServer) getSysLoginLogPage(c *gin.Context, cmd systemlog.GetSysLoginLogPageCommand) {
	result, count, err := hs.systemLogService.GetSysLoginLogPage(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithData(c, helper.Page{CurrentPage: cmd.PageIndex, PageSize: cmd.PageSize, List: result, Total: count})
}
func (hs *HTTPServer) getSysOperaLogPage(c *gin.Context, cmd systemlog.GetSysOperaLogPageCommand) {
	result, count, err := hs.systemLogService.GetSysOperaLogPage(c, cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithData(c, helper.Page{CurrentPage: cmd.PageIndex, PageSize: cmd.PageSize, List: result, Total: count})
}

// GetOnlineUser @Summary 获取在线用户列表
// @Description 获取在线用户列表
// @Tags 系统监控
// @Accept json
// @Produce json
// @Param userName query string false "用户名"
// @Param beginTime query string false "开始时间"
// @Param endTime query string false "结束时间"
// @Param pageIndex query int false "当前页"
// @Param pageSize query int false "每页数量"
// @Success 200 {object} helper.Page{List=[]onlineuser.OnlineCommand} "成功"
// @Failure 500 {object} helper.ErrorResponse "失败"
// @Router /monitor/online [get]
func (hs *HTTPServer) GetOnlineUser(c *gin.Context) {
	cmd := onlineuser.GetOnlinePageCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	result, count, err := hs.onlineService.GetOnlineUserPage(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithData(c, helper.Page{CurrentPage: cmd.PageIndex, PageSize: cmd.PageSize, List: result, Total: count})
}

// InvalidateSession @Summary 强制退出用户会话
// @Description 强制退出用户会话
// @Tags 系统监控
// @Accept json
// @Produce json
// @Param token query string false "token"
// @Success 200 {object} helper.SuccessResponse "成功"
// @Failure 500 {object} helper.ErrorResponse "失败"
// @Router /monitor/online/:token [del]
func (hs *HTTPServer) Kickout(c *gin.Context) {
	token := c.Param("token")
	if token == "" {
		helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, constant.ErrInvalidParams)
		return
	}
	err := hs.onlineService.Remove(c.Request.Context(), token)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}
